<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Video extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $id = $this->request->post('id');
        $url = model('Video')->videoOper($id);
        return json_encode($url);
    }
    // 列表页
    public function test(){
        $subjectId = $this->request->post('subjectId');
        $openid = $this->request->post('openid');
        $videoclass_id = $this->request->post('id');
        $student = model('Student')->packageId($openid);
        if($student['package_id'] <= 1){
            return 401;
        }
        $res = Db::name('video')->where('subject_id',$subjectId)->where(function ($query) use($videoclass_id){
            if($videoclass_id > 0 && $videoclass_id < 5){
                $query->where('jy_video.videoclass_id',$videoclass_id);
            }
        })->select();
        foreach ($res as $k=>$v){
            $length = model('Student')->getVideoInfo('http://yinglan.cdylsr.com'.$v['videofile']);
            $res[$k]['min'] = $length['min'];
            $res[$k]['sec'] = (int)$length['seconds'];
            $count = Db::name('history')->where("video_id = {$v['id']} and student_id = {$student['id']}")->value('count');
            $res[$k]['myMin'] = floor($count/60);
            $res[$k]['mySec'] = $count%60;
        }
        return json_encode($res);
    }
    // 视频播放记录
    public function history(){
        $params = $this->request->post();
        $student = model('Student')->packageId($params['openid']);
        $count = Db::name('history')->where('student_id',$student['id'])->where('video_id',$params['video_id'])->count();
        if($count){
            $res = Db::name('history')->where('student_id',$student['id'])->where('video_id',$params['video_id'])->update(['count'=>$params['count']]);
        }else{
            $arr = array(
                'video_id'  => $params['video_id'],
                'student_id'=> $student['id'],
                'count'     => $params['count'],
                'createtime'=> time()
            );
            $res = Db::name('history')->insert($arr);
        }
        return $res?200:400;
    }

}
